// $codepro.audit.disable numericLiterals, methodChainLength
/**
 * Class: NURSE PANEL
 * Description:
 * 
 */
import java.awt.BorderLayout;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.JScrollPane;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;



/**
 * @author Team Awesome Express
 * @version $ Revision 1.0 $
 */
public class NursePanel extends JPanel {
	
	/**
	 * Field myThreadPool.
	 */
	private static ExecutorService MyThreadPool = Executors.newCachedThreadPool();

	/**
	 * Field myDataManager.
	 */
	private DataManager myDataManager;

	/**
	 * Field viewAppt.
	 */
	private ViewAppointment viewAppt;

	/**
	 * Field treatRecord.
	 */
	private CreateTreatmentRecord treatRecord;

	/**
	 * Field patientList.
	 */
	private JList patientList;

	/**
	 * Field patientArray.
	 */
	private Patient[] patientArray;

	/**
	 * Field newPatient.
	 */
	private CreateAccount newPatient;

	/**
	 * Field frame.
	 */
	private static JFrame Frame;

	/**
	 * Field scrollPane.
	 */
	private final JScrollPane scrollPane = new JScrollPane();
	
	/**
	 * Field numPatients.
	 */
	private static final int NUMPATIENTS = 0;

	/**
	 * Constructor for NursePanel.
	 * @param name String
	 * @param myDataManager DataManager
	 */
	public NursePanel(String name, final DataManager myDataManager) {
		setBackground(GlobalVars.BACKGROUND);
		this.myDataManager = myDataManager;

		final JButton btnCreateEdit = new JButton("Create New Patient");
		btnCreateEdit.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) { // If the view / edit Patients button is Pressed
				patientButtonPressed(); // will call method that calls initialize table on datamanager
			}
		});
		add(btnCreateEdit, BorderLayout.NORTH);
		
		final JButton btnViewAppt = new JButton("View appointment page");
		btnViewAppt.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) { 
				viewApptButtonPressed();
			}
		});
		
		final JButton btnCreateTreatmentRecord = new JButton("Create New Treatment Record"); // Treatment button Action Handeler
		btnCreateTreatmentRecord.addActionListener(new ActionListener(){
			
			public void actionPerformed(ActionEvent arg0) { 
				treatmentButtonPressed();
			}
		});
		
		final JButton btnLookupPatient = new JButton("Lookup Patients"); //Used to lookup patients by name
		btnLookupPatient.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0){
				lookupPatientButtonPressed();
			}
		});

		final JLabel welcomeLabel = new JLabel("Welcome Nurse " + name);
		
		final JLabel headerLabel = new JLabel("Double click a patient to open patient profile");
		
		final JLabel label = new JLabel("Nurses@Work");
		label.setIcon(new ImageIcon("./src/nursesWork.jpg"));

		final JButton btnLogOut = new JButton("Log Out");
		btnLogOut.addMouseListener(new MouseAdapter() { // Log out button pressed
	
			public void mousePressed(MouseEvent arg0) {
				Frame.dispose(); // Close Nurse Panel
				LoginFrame.createWindow(); // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.unusedReturnValue
			}
		});
		
		final JButton btnRefresh = new JButton("Refresh");
		btnRefresh.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0){
				refreshButtonPressed(scrollPane, NUMPATIENTS);

			}
		});

		final GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
							.addGroup(groupLayout.createSequentialGroup()
								.addContainerGap()
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
									.addComponent(btnLookupPatient, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(btnCreateEdit, GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE))
								.addGap(31)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
									.addComponent(btnCreateTreatmentRecord, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(btnViewAppt, GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE))
								.addGap(10))
							.addGroup(groupLayout.createSequentialGroup()
								.addGap(23)
								.addComponent(welcomeLabel)
								.addPreferredGap(ComponentPlacement.RELATED, 218, Short.MAX_VALUE)
								.addComponent(label, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE)))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(129)
							.addComponent(btnLogOut, GroupLayout.PREFERRED_SIZE, 247, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(5)
							.addComponent(btnRefresh)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(headerLabel)
								.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 268, GroupLayout.PREFERRED_SIZE))))
					.addGap(324))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(10)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(label, GroupLayout.PREFERRED_SIZE, 57, GroupLayout.PREFERRED_SIZE)
						.addComponent(welcomeLabel))
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addGroup(groupLayout.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(headerLabel, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 128, GroupLayout.PREFERRED_SIZE)
							.addGap(18))
						.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(btnRefresh)
							.addGap(78)))
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnCreateEdit)
						.addComponent(btnViewAppt))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnLookupPatient)
						.addComponent(btnCreateTreatmentRecord, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(btnLogOut)
					.addGap(42))
		);
	     final Future future = MyThreadPool.submit(new Runnable() {
	            public void run() {
	                refreshButtonPressed(scrollPane, NUMPATIENTS);
	            }});
		//patientList = new JList(patientArrayList.toArray()); // Display the arraylist in the NursePanel
		final String[] tempArray = {"     Loading Patients List..."};
		patientList = new JList(tempArray);
		scrollPane.setViewportView(patientList);
		patientList.addMouseListener(new MouseAdapter() { // Adds support for double clicking
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					final int index = patientList.locationToIndex(e.getPoint()) + 1;
					PatientInformationPanel.createWindow(myDataManager, patientArray[index]); // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.unusedReturnValue
				}
			}
		});
		setLayout(groupLayout);

		
	}

	/**
	 * Will open panel for nurse to create patients
	 */
	public void patientButtonPressed() {
		newPatient = new CreateAccount(myDataManager);
	}
	
	/**
	 * Will create a jframe to view the appointments
	 */
	public void viewApptButtonPressed() {
		viewAppt = new ViewAppointment(myDataManager);
	}
	
	/**
	 * Method treatmentButtonPressed.
	 */
	public void treatmentButtonPressed() {
		final int index = patientList.getSelectedIndex() + 1;
		treatRecord = new CreateTreatmentRecord(myDataManager, null, -1, patientArray[index]);
	}
	
	/**
	 * Method lookupPatientButtonPressed.
	 */
	public void lookupPatientButtonPressed(){
		LookupPanel.createWindow("Lookup Patient", myDataManager); // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.unusedReturnValue
	}

	/**
	 * Method refreshButtonPressed.
	 * @param scrollPane JScrollPane
	 * @param numPatients int
	 */
	public void refreshButtonPressed(JScrollPane scrollPane, int numPatients){
		final ArrayList refreshPatientArrayList = new ArrayList();
		final int currentNumPatients = myDataManager.getNumRows(GlobalVars.PATIENT_SHEET) + 1;
		if(currentNumPatients == numPatients){
			patientList.repaint();
			scrollPane.repaint();
			return;
		}
		else{
			patientArray = getPatients(currentNumPatients); // Holds list of patient objects
				
			for (int i = 1; i < patientArray.length; i++) { 
				refreshPatientArrayList.add(fillPatientArray(patientArray[i])); // This will put the  first name, and last name into one index of an arraylist to display
				}
		patientList = new JList(refreshPatientArrayList.toArray()); // Display the arraylist in the NursePanel
		patientList.addMouseListener(new MouseAdapter() { // Adds support for double clicking
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					final int index = patientList.locationToIndex(e.getPoint()) + 1;
					PatientInformationPanel.createWindow(myDataManager, patientArray[index]); // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.unusedReturnValue
				}
			}
		});
		patientList.revalidate();
		scrollPane.revalidate();
		patientList.repaint();
		scrollPane.repaint();
		scrollPane.setViewportView(patientList);
		numPatients = currentNumPatients; // $codepro.audit.disable questionableAssignment
		}
		
	}
	
	/**
	 *  Will return an array list of patients in the given row numbers
	 * @param rowNumbers The row numbers to grab data from in the datamanager
	
	 * @return An array list of patients */
	private Patient[] getPatients(int rowNumbers){
		final Patient[] patientsArray = new Patient[rowNumbers];
		final String[][] rawData = new String[rowNumbers][];
		for (int i = 0; i < rowNumbers; i++){
			rawData[i] = myDataManager.getDataAtRow(
						GlobalVars.PATIENT_SHEET,
						1,
						12,
						i + 1
						 );
			patientsArray[i] = new Patient(rawData[i]); 
		}
		
		return patientsArray;
	}
	
	/**
	 * Will grab login name, name, and lastname to display in patient list
	 * @param p A person Object
	
	 * @return login name, name, and lastname to display in patient list */
	  public String fillPatientArray(Person p) {
		  return p.getName() + " " + p.getLastname();
	  }
	
	/**
	 * Method createWindow.
	 * @param name String
	 * @param myDataManager DataManager
	
	 * @return JFrame */
	public static JFrame createWindow(String name, DataManager myDataManager) {
		Frame = new JFrame();
		Frame.setSize(564, 420);
		Frame.getContentPane().add(new NursePanel(name, myDataManager));
		Frame.setTitle("Welcome " + name);
		Frame.setVisible(true);
		Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		return Frame;
		}
} // end NursePanel
